package com.guchenbo.example.socket.bio;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocketFactory;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.KeyStore;

/**
 * @Author gucb
 * @Date 15/09/2017.
 */
public class HttpsServer {

    public static void main(String[] args) {
        try {
            KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());//jks
            String key = System.getProperty("java.home") + "/lib/security/cacerts";
            keyStore.load(new FileInputStream(key), null);

            KeyManagerFactory keyManagerFactory = KeyManagerFactory
                            .getInstance(KeyManagerFactory.getDefaultAlgorithm());//SunX509，x509是一个标准
            keyManagerFactory.init(keyStore, null);
            SSLContext sslContext = SSLContext.getInstance("SSLv3");
            sslContext.init(keyManagerFactory.getKeyManagers(), null, null);

            SSLServerSocketFactory socketFactory = sslContext.getServerSocketFactory();
            ServerSocket serverSocket = socketFactory.createServerSocket(8889);

            Socket socket = serverSocket.accept();
            InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream();

            String s = "hello world";
            outputStream.write(s.getBytes());
            outputStream.flush();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
